<?php
class Noticias extends Controller{
	function Noticias(){
		parent::Controller();
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->helper('date');
		$this->load->helper('text');
		$this->load->model('noticias_model');
	}
	function index(){
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		$this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
		$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
		$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
		$this->output->set_header("Pragma: no-cache"); 
		$this->load->view('mod_noticias/index.php');
	}
	function addNotice(){
		$configure = array(
			array(
				'field' => 'titulo', 
				'label' => 'Título',
				'rules' => 'required|max_length[200]'
			),
			array(
				'field' => 'notice', 
				'label' => 'Noticia',
				'rules' => 'required'
			)
		);
		$field = $this->input->post('notice');
		$salida = '';
		$flag = 1;
		$this->form_validation->set_rules($configure);
		if($this->form_validation->run() == FALSE){
			$salida = validation_errors('<div style="color:#C00; font-size:100%;">', '</div>');
		}
		else{
			$fechaFormat = "%Y-%m-%j";
			$fechaPub = mdate($fechaFormat);
			$rutaCompleta = '';
			//Si tiene imagenes el POST...
			if(!empty($_FILES['imagen']['name'])){
				$config['upload_path'] = './img_noticias/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size'] = '1024';
				$this->load->library('upload', $config);
				$flag = 0;
				/*$old = umask(0);
				chmod($config['upload_path'], 0777);
				umask($old);*/
				if ( ! $this->upload->do_upload('imagen')){
					$salida = $this->upload->display_errors('<div style="color:#C00; font-size:100%;">', '</div>');
				}	
				else{
					$file = array('upload_data' => $this->upload->data());
					$nombreArchivo = $file['upload_data']['file_name'];
					$rutaCompleta = $config['upload_path'].''.$file['upload_data']['file_name'];
					$extension = $file['upload_data']['file_ext'];
					$salida = "<div style='color:#3c8728; font-size:100%;'>La noticia se publicó correctamente con la imágen ".$_FILES['imagen']['name'].".</div> ";
				}
			}
			$var = array(
				'idtabla_noticia' => '',
				'fecha_pub' => $fechaPub,
				'nusuario_pub' => $this->session->userdata('userName'),
				'titulo' =>  $this->input->post('titulo'),
				'noticia' => $this->input->post('notice'),
				'ruta_img' => $rutaCompleta,
			);
			if($flag == 1){
				$salida = '<div style="color:#3c8728; font-size:100%;">La noticia se publicó correctamente.</div>';
			}
			if(!$this->noticias_model->addNotice($var))
				$salida = '<div style="color:#C00; font-size:100%;">Se produjo un error al intentar almacenar la noticia en la Base de datos, por favor intentelo nuevamente.</div>';
		}
		echo $salida;
	}
	
	function reloadNotice(){
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		$this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
		$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
		$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
		$this->output->set_header("Pragma: no-cache"); 
		$this->load->view('mod_noticias/notice');
	}
	function readMore(){
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		$this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
		$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
		$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
		$this->output->set_header("Pragma: no-cache"); 
		$id = $this->uri->segment(4);
		
		$data['notice'] = $this->noticias_model->getOneNotice($id);
		$this->load->view('mod_noticias/readmore',$data);
	}
	function reloadComment(){
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		$this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
		$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
		$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
		$this->output->set_header("Pragma: no-cache"); 
		$id = $this->uri->segment(4);
		$data['id'] = $id;
		//$data['comments'] = $this->noticias_model->getComments($id);
		$this->load->view('mod_noticias/viewcomment',$data);
	}
	function reloadPendentComment(){
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		$this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
		$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
		$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
		$this->output->set_header("Pragma: no-cache"); 
		$id = $this->uri->segment(4);
		$data['id'] = $id;
		//$data['comments'] = $this->noticias_model->getComments($id, FALSE);
		$this->load->view('mod_noticias/pendentcomments',$data);
	}
	function addComment(){
		$var = array(
			array(
				'field' => 'nombre', 
				'label' => 'Nombre',
				'rules' => 'required'
			),
			array(
				'field' => 'comentario', 
				'label' => 'Comentario',
				'rules' => 'max_length[500]'
			),
		
		);
		$this->form_validation->set_rules($var);
		if($this->form_validation->run() == FALSE){
			$salida = validation_errors('<div style="color:#C00; font-size:100%;">', '</div>');
		}
		else{
			$id = $this->input->post('id');
			$fecha = mdate("%Y-%m-%j");
			$vars = array(
				'idtabla_comentario' => '',
				'nombre' => $this->input->post('nombre'),
				'comentario' => $this->input->post('comentario'),
				'fechacomentario' => $fecha,
				'idtabla_noticia' => $id
			);
			if($this->noticias_model->addComment($vars)){
				$salida = 'Su comentario será filtrado por el personal del tribunal. Agradecemos su comprensión';
			}
			else{
				$salida = '<div style="color:#C00; font-size:100%;">Ha ocurrio un error al agregar el comentario</div>';
			}
		}
		echo $salida;
	}
	
	function delComment(){
		$id = $this->uri->segment(4);
		if($this->noticias_model->delComment($id))
			echo '<div style="color:#3c8728; font-size:100%;">El comentario fue borrado exitosamente!.</div>';
		else
			echo '<div style="color:#C00; font-size:100%;">Ha ocurrio un error al eliminar el comentario. Intentelo nuevamente</div>';
	}
	
	function pub_comment(){
		$id = $this->uri->segment(4);
		$data = array(
			'publicado' => '1'
		);
		$this->noticias_model->pubComment($data, $id);
	}
	function delNotice(){
		$id = $this->uri->segment(4);
		$ruta = $this->noticias_model->getImgNoticia($id);
		if(!empty($ruta->ruta_img))
			unlink($ruta->ruta_img);
		if($this->noticias_model->del_noticia($id)){
			echo "Noticia eliminada exitosamente";			
		}
		else
			echo "Ocurrio un error al eliminar la noticia";
	}
	function modNoticia(){
	$id = $this->input->post('id');
	$configure = array(
			array(
				'field' => 'title', 
				'label' => 'Título',
				'rules' => 'required|max_length[200]'
			),
			array(
				'field' => 'notice', 
				'label' => 'Noticia',
				'rules' => 'required'
			)
		);
		$flag = 0;
		$this->form_validation->set_rules($configure);
		if($this->form_validation->run() == FALSE){
			$salida = validation_errors('<div style="color:#C00; font-size:100%;">', '</div>');
		}
		else{
			$fechaFormat = "%Y-%m-%j";
			$fechaPub = mdate($fechaFormat);
			$ruta = $this->noticias_model->getImgNoticia($id);
			$rutaCompleta = '';
			$delete = FALSE;
			if(!empty($ruta->ruta_img))
				$rutaCompleta = $ruta->ruta_img;
			if($this->input->post('del') == 1){
				unlink($rutaCompleta);
				$rutaCompleta = '';
				$delete = TRUE;
			}
			//Si tiene imagenes el POST...
			if(!empty($_FILES['img']['name'])){
				$config['upload_path'] = './img_noticias/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size'] = '1024';
				$this->load->library('upload', $config);
				$flag = 1;
				/*$old = umask(0);
				chmod("./img_noticias/", 0777);
				umask($old);*/
			
				if ( ! $this->upload->do_upload('img')){
					$salida1 = $this->upload->display_errors();
				}	
				else{
					$file = array('upload_data' => $this->upload->data());
					$nombreArchivo = $file['upload_data']['file_name'];
					//borramos la imagen anterior si es que hay una
					if(!empty($ruta->ruta_img) && $delete == FALSE)
						unlink($rutaCompleta);
					$rutaCompleta = $config['upload_path'].''.$file['upload_data']['file_name'];
					$extension = $file['upload_data']['file_ext'];
					$salida1 = "Se agregó la imágen ".$_FILES['img']['name'].". ";
					
				}
			}
			$data = array(
				'fecha_pub' => $fechaPub,
				'titulo' =>  $this->input->post('title'),
				'noticia' => $this->input->post('notice'),
				'ruta_img' => $rutaCompleta
			);
			if($this->noticias_model->updateNotice($id, $data)){
				echo "Noticia modificada con exito. ";
				if($flag == 1)
					echo "\n".$salida1;			
			}
			else{
				echo "Ocurrio un error al modificar la noticia, intentelo nuevamente";
				if($flag == 1)
					echo "\n".$salida1;
			}	
		}
	}
}
?>